<template>
  <div>
    <!-- 搜索框 -->
    <TopFilter ref="form" :list="form" :cols="3" :labelWidth="'85'" @filterChange="changeHandle" />
    <FilterTable
      ref="table"
      height="300px"
      :columns="columns"
      columnsRef="partPartOutboundAdjustOutboundAdjustOrder"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.onChangeColumn = arr)"
      selectionType="single"
    ></FilterTable>

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" :loading="addBtnState" @click="saveDialog">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { getDealerUser } from '@/api/part/partInventory/partInventory/index.js';
import { dictionary } from '@/utils/dictMixin';
import { searchAdjustOrder, isLocked } from '@/api/part/partOutbound/adjustOutbound/index';
export default {
  name: 'adjustOrder',
  mixins: [dictionary],
  props: ['orderList', 'ptype'],
  data() {
    // this.BaseTable = null;
    return {
      form: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: searchAdjustOrder, // 调用接口
      fetchParams: { xhrAbort: true },
      selectedRows: [],
      addBtnState: null
    };
  },
  mounted() {
    this.getEmp();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '借出单号',
          fieldName: 'allocateOutNo',
          initialValue: this.orderList
        },
        {
          type: 'RANGE_DATE',
          label: '入账日期',
          fieldName: 'innerStr|innerEnd'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          },
          sorter: true
        },
        {
          title: '借出出库单号',
          dataIndex: 'ALLOCATE_OUT_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '客户代码',
          dataIndex: 'CUSTOMER_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '客户名称',
          dataIndex: 'CUSTOMER_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 150,
          showOverflowTooltip: true
        },
        {
          title: '开单日期',
          dataIndex: 'CREATED_AT',
          dateFormat: 'YYYY-MM-DD',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          showOverflowTooltip: true
        },
        {
          title: '经手人',
          dataIndex: 'HANDLER',
          sorter: true,
          editable: false,
          editType: 'select',
          editItems: [],
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '锁定人',
          dataIndex: 'LOCK_USER',
          sorter: true,
          editable: false,
          editType: 'select',
          editItems: [],
          filter: true,
          filterType: 'input'
        }
        // {
        //   title: '是否呆滞借出',
        //   dataIndex: 'SO_NO',
        //   sorter: true,
        // },
        // {
        //   title: '是否网内借出',
        //   dataIndex: 'VIN',
        //   sorter: true,
        // }
      ];
    },
    changeHandle(val) {
      console.log(val, 'val');
      let obj = _.cloneDeep(val);
      if (obj.innerStr) {
        obj.innerStr = obj.innerStr.substr(0, 10);
      }
      if (obj.innerEnd) {
        obj.innerEnd = obj.innerEnd.substr(0, 10);
      }
      console.log(obj, 'obj');
      this.fetchParams = Object.assign({}, obj, { ptype: this.ptype ? this.ptype : null });
    },
    // 获取员工信息并转义
    async getEmp() {
      const res = await getDealerUser();
      this.columns.find(x => x.dataIndex === 'HANDLER').editItems = res.data.map(x => ({ value: x.userId + '', text: x.employeeName }));
      this.columns.find(x => x.dataIndex === 'LOCK_USER').editItems = res.data.map(x => ({ value: x.userId + '', text: x.employeeName }));
    },
    // searchAdjustOrder(val) {
    //   console.log('点击了')
    //   const params = Object.assign({}, val, { offset: 0, xhrAbort: false });
    //   this.fetchParams = params;
    // },
    // 查询
    // queryClick() {
    //   this.$refs.formPanel.SUBMIT_FORM();
    // },
    // 重置
    // resetClick() {
    //   this.$refs.formPanel.RESET_FORM();
    // },
    async saveDialog() {
      if (this.onChangeColumn && this.onChangeColumn.length) {
        let obj = this.onChangeColumn[0];
        const soNO = obj.ADJUST_OUTBOUND_NO;
        if (obj.LOCK_USER != null && obj.LOCK_USER != '') {
          // 有锁定人弹出提示
          return this.$notify.warning({ title: '提示信息', message: '该单号已被' + this.onChangeColumn[0].LOCK_USER + '锁定' });
        } else {
          // 没有进行单号锁定再传值
          // let res = await isLocked(soNO);
          // if (res.resultCode == 200) {
          this.$emit('saveDialog', this.onChangeColumn);
          this.closeDialog();
          // }
        }
      }
    },
    //  点击取消关闭窗口
    closeDialog() {
      this.$emit('close', false);
    }
  }
};
</script>
<style lang="less" scoped></style>
